package com.sc.admin.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sc.admin.mapper.UserRoleMapper;
import com.sc.admin.pojo.UserRole;

@Service
public class UserRoleServiceImpl implements UserRoleService {
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Override
	public void deleteUserRole(int userId) {
		userRoleMapper.deleteByUserId(userId);
	}

	@Override
	public void addUserRole(int userId, String roleIds) {
		userRoleMapper.deleteByUserId(userId);
		if(StringUtils.isEmpty(roleIds)) {
			return;
		}
		String[] split = roleIds.split(",");
		for(String s : split) {
			UserRole record = new UserRole();
			record.setUserId(userId);
			record.setRoleId(Integer.parseInt(s));
			userRoleMapper.insert(record );
		}
	}

	@Override
	public List<UserRole> getUserRolesByUserId(int userId) {
		return userRoleMapper.selectByUserId(userId);
	}

}
